import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
model_path ="/root/autodl-tmp/Qwen/Qwen3-1.7B"
# 加载原下载路径的tokenizer和model
tokenizer = AutoTokenizer.from_pretrained(model_path, 
                                                   use_fast=False, 
                                                 trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(model_path, 
                                                  device_map="auto", 
                                                 torch_dtype=torch.bfloat16)
lora_model_path = "/root/autodl-tmp/output_qwen3/checkpoint-750"
model = PeftModel.from_pretrained(base_model, model_id=lora_model_path)
def predict(messages, model, tokenizer):
    device = "cuda"
    text = tokenizer.apply_chat_template(messages, 
                                         tokenize=False, 
                                         add_generation_prompt=True)
    model_inputs = tokenizer([text], return_tensors="pt").to(device)
    gen_ids = model.generate(model_inputs.input_ids, max_new_tokens=2048)
    generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids 
                                                       in zip(model_inputs.input_ids, gen_ids)]
    response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return response

test_texts = {
    'instruction': "你是一个医学专家,你需要根据用户的问题,给出带有思考的回答。",
    'input': "医生,我最近被诊断为糖尿病,听说碳水化合物的选择很重要,我应该选择什么样的碳水化合物呢？"
}
instruction = test_texts['instruction']
input_value = test_texts['input']
messages = [
    {"role": "system", "content": f"{instruction}"},
    {"role": "user", "content": f"{input_value}"}
]
response = predict(messages, model, tokenizer)
print(response)
